home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / kant-generator-04-c / Kant ƒ / Kode ƒ / kant search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.9 KB  |  207 lines  |  [TEXT/MMCC]

  1. #include "kant search.h"
  2. #include "kant text twiddling.h"
  3. #include "dialogs.h"
  4. #include "util.h"
  5. #include "prefs.h"
  6. #include "program globals.h"
  7.  
  8. #define findDialog    400
  9.  
  10. /* NUM_FIND is defined in program globals.h */
  11.  
  12. Str255            gFindString[NUM_FIND];
  13. Str255            gReplaceString[NUM_FIND];
  14. short            gLastFindPosition;
  15. short            gWhichFound;
  16.  
  17. static    Boolean SearchDoneQQ(short *strPos, short *strLen);
  18.  
  19. void SetLastFindPosition(short val)
  20. {
  21.     gLastFindPosition=val;
  22.     gWhichFound=-1;
  23. }
  24.  
  25. short SearchForwards(TEHandle hTE, short *foundLength)
  26. {
  27.     short            i;
  28.     short            offset;
  29.     short            max;
  30.     short            strPos[NUM_FIND];
  31.     short            strLen[NUM_FIND];
  32.     unsigned char    *a;
  33.     
  34.     max=(**hTE).teLength;
  35.     HLock((**hTE).hText);
  36.     a=(unsigned char*)(*((**hTE).hText));
  37.     for (i=0; i<NUM_FIND; i++)
  38.     {
  39.         strPos[i]=1;
  40.         strLen[i]=gFindString[i][0];
  41.     }
  42.     
  43.     offset=gLastFindPosition;
  44.     while ((offset<max) && (!SearchDoneQQ(strPos, strLen)))
  45.     {
  46.         for (i=0; i<NUM_FIND; i++)
  47.         {
  48.             if (a[offset]==gFindString[i][strPos[i]])
  49.                 strPos[i]++;
  50.             else
  51.                 strPos[i]=1;
  52.         }
  53.         offset++;
  54.     }
  55.     
  56.     HUnlock((**hTE).hText);
  57.     for (i=0; i<NUM_FIND; i++)
  58.     {
  59.         if ((strLen[i]>0) && (strPos[i]>strLen[i]))
  60.         {
  61.             gLastFindPosition=offset;
  62.             *foundLength=strLen[i];
  63.             return offset-strLen[i];
  64.         }
  65.     }
  66.     
  67.     return -1;
  68. }
  69.  
  70. Boolean SearchDoneQQ(short *strPos, short *strLen)
  71. {
  72.     Boolean            done;
  73.     short            i;
  74.     
  75.     done=FALSE;
  76.     for (i=0; i<NUM_FIND; i++)
  77.     {
  78.         if (gFindString[i][0]!=0x00)
  79.             done|=(strPos[i]>strLen[i]);
  80.     }
  81.     
  82.     return done;
  83. }
  84.  
  85. #if 0
  86.  
  87. short SearchBackwards(short startPosition, Str255 theStr, TEHandle hTE)
  88. /* note that startPosition is the starting position for looking for the last character in theStr */
  89. {
  90.     short            i;
  91.     short            strPos;
  92.     short            strLen;
  93.     unsigned char    *a;
  94.     
  95.     i=startPosition;
  96.     HLock((**hTE).hText);
  97.     a=(unsigned char*)(*((**hTE).hText));
  98.     strLen=theStr[0];
  99.     strPos=strLen;
  100.     while ((i>=0) && (strPos>0))
  101.     {
  102.         if (a[i]==theStr[strPos])
  103.             strPos--;
  104.         else
  105.             strPos=strLen;
  106.         i--;
  107.     }
  108.     
  109.     HUnlock((**hTE).hText);
  110.     if (strPos==0)
  111.         return i+1;
  112.     else
  113.         return -1;
  114. }
  115.  
  116. #endif
  117.  
  118. Boolean DoFindDialog(void)
  119. {
  120.     DialogPtr        theDlog;
  121.     short            itemSelected;
  122.     Handle            itemH;
  123.     short            itemType;
  124.     Rect            box;
  125.     short            i;
  126.     Str255            theStr;
  127.     ModalFilterUPP    procFilter = NewModalFilterProc(TwoButtonFilter);
  128.     
  129.     PositionDialog('DLOG', findDialog);
  130.     theDlog = GetNewDialog(findDialog, 0L, (WindowPtr)-1L);
  131.  
  132.     GetDItem(theDlog, 3, &itemType, &itemH, &box);
  133.     InsetRect(&box, -4, -4);
  134.     SetDItem(theDlog, 3, itemType, (Handle)OutlineDefaultButton, &box);
  135.     
  136.     for (i=0; i<NUM_FIND; i++)
  137.     {
  138.         GetDItem(theDlog, 6+i, &itemType, &itemH, &box);
  139.         SetIText(itemH, gFindString[i]);
  140.         GetDItem(theDlog, 10+i, &itemType, &itemH, &box);
  141.         SetIText(itemH, gReplaceString[i]);
  142.     }
  143.     
  144.     SetWTitle((WindowPtr)theDlog, "\pMulti-find");
  145.     ShowWindow(theDlog);
  146.     itemSelected=0;
  147.     while ((itemSelected!=1) && (itemSelected!=2))
  148.     {
  149.         ModalDialog(procFilter, &itemSelected);
  150.     }
  151.     
  152.     if (itemSelected==1)
  153.     {
  154.         for (i=0; i<NUM_FIND; i++)
  155.         {
  156.             GetDItem(theDlog, 6+i, &itemType, &itemH, &box);
  157.             GetIText(itemH, theStr);
  158.             Mymemcpy((Ptr)gFindString[i], (Ptr)theStr, theStr[0]+1);
  159.             GetDItem(theDlog, 10+i, &itemType, &itemH, &box);
  160.             GetIText(itemH, theStr);
  161.             Mymemcpy((Ptr)gReplaceString[i], (Ptr)theStr, theStr[0]+1);
  162.         }
  163.     }
  164.  
  165.     HideWindow(theDlog);
  166.     DisposeDialog(theDlog);
  167.  
  168.     if (itemSelected==1)
  169.     {
  170.         SaveThePrefs();
  171.         gLastFindPosition=0;
  172.         return DoFindAgain();
  173.     }
  174.     
  175.     return FALSE;
  176. }
  177.  
  178. Boolean DoFindAgain(void)
  179. {
  180.     TEHandle        hTE;
  181.     short            newOffset;
  182.     short            len;
  183.     
  184.     hTE=GetTheTextHandle();
  185.     newOffset=SearchForwards(hTE, &len);
  186.     if (newOffset!=-1)
  187.     {
  188.         TESetSelect(newOffset, newOffset+len, hTE);
  189.         TESelView(hTE);
  190.         return TRUE;
  191.     }
  192.     
  193.     return FALSE;
  194. }
  195.  
  196. Boolean AnythingToFindQQ(void)
  197. {
  198.     return ((gFindString[0][0]!=0x00) || (gFindString[1][0]!=0x00) ||
  199.             (gFindString[2][0]!=0x00) || (gFindString[3][0]!=0x00));
  200. }
  201.  
  202. Boolean AnythingToReplaceQQ(void)
  203. {
  204.     return ((gReplaceString[0][0]!=0x00) || (gReplaceString[1][0]!=0x00) ||
  205.             (gReplaceString[2][0]!=0x00) || (gReplaceString[3][0]!=0x00));
  206. }
  207.